package top.lyc829.aircontroller.ui.index

import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import top.lyc829.aircontroller.MyApp
import top.lyc829.aircontroller.logic.dao.AirInfo
import top.lyc829.aircontroller.logic.network.AllResponse
import top.lyc829.aircontroller.logic.network.HttpUtil
import top.lyc829.aircontroller.logic.network.TemAndHumInfo
import top.lyc829.aircontroller.logic.network.TemAndHumInfoResponse
import java.io.IOException

class MainViewModel: ViewModel() {

    val isLogin: LiveData<Boolean>
    get() = _isLogin

    val airInfo: LiveData<AirInfo>
    get() = _airInfo

    val temAndHum: LiveData<TemAndHumInfo>
    get() = _temAndHum

    private val _isLogin = MutableLiveData<Boolean>()
    private val _airInfo = MutableLiveData<AirInfo>()
    private val _temAndHum = MutableLiveData<TemAndHumInfo>()

    init {
        val airInfoTmp = AirInfo("off", "hot", "26")
        _airInfo.value = airInfoTmp
    }

    fun updateLoginState() {
        _isLogin.value = MyApp.isLogin
    }

    fun refreshTemAndHum() {
        HttpUtil.getHttp("/get_hum_and_tem", object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d("MyTag", e.toString())
                Looper.prepare()
                Toast.makeText(MyApp.context, "网络开小差了...", Toast.LENGTH_LONG).show()
                Looper.loop()
            }

            override fun onResponse(call: Call, response: Response) {
                response.body?.let { _temAndHum.postValue(parseTemAndHum(it.string())) }
            }
        })
    }

    private fun parseTemAndHum(jsonStr: String): TemAndHumInfo {
        val gson = Gson()
        return gson.fromJson(jsonStr, TemAndHumInfoResponse::class.java).data
    }


    private fun sendControlRequest() {
        Log.d("MyTag", "send")
        val map = mutableMapOf(
                "state" to _airInfo.value!!.state,
                "mode" to _airInfo.value!!.mode,
                "tem" to _airInfo.value!!.tem
        )
        HttpUtil.postHttp("/air_controller", map, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d("MyTag", e.toString())
                Looper.prepare()
                Toast.makeText(MyApp.context, "网络开小差了...", Toast.LENGTH_LONG).show()
                Looper.loop()
            }

            override fun onResponse(call: Call, response: Response) {
                response.body?.let { Log.d("MyTag", it.string()) }
            }

        })
    }


    fun addTem() {
        if (_airInfo.value!!.state == "on" && _airInfo.value!!.tem.toInt() < 32) {
            _airInfo.value = AirInfo(_airInfo.value!!.state, _airInfo.value!!.mode, _airInfo.value!!.tem.toInt().plus(1).toString())
            sendControlRequest()
        }
    }

    fun reduceTem() {

        if (_airInfo.value!!.state == "on" && _airInfo.value!!.tem.toInt() > 16) {
            _airInfo.value = AirInfo(_airInfo.value!!.state, _airInfo.value!!.mode, _airInfo.value!!.tem.toInt().minus(1).toString())
            sendControlRequest()
        }
    }

    fun changeMode() {
        if (_airInfo.value!!.state == "on") {
            _airInfo.value = when (_airInfo.value?.mode) {
                "cold" -> AirInfo(_airInfo.value!!.state, "hot", _airInfo.value!!.tem)
                "hot" -> AirInfo(_airInfo.value!!.state, "cold", _airInfo.value!!.tem)
                else -> AirInfo(_airInfo.value!!.state, "cold", _airInfo.value!!.tem)
            }
            sendControlRequest()
        }



    }

    fun changeOnOff() {
        _airInfo.value = when (_airInfo.value?.state) {
            "off" -> AirInfo("on", _airInfo.value!!.mode, _airInfo.value!!.tem)
            "on" -> AirInfo("off", _airInfo.value!!.mode, _airInfo.value!!.tem)
            else -> AirInfo("off", _airInfo.value!!.mode, _airInfo.value!!.tem)
        }

        sendControlRequest()
    }
}

